#include "zygcanvas.h"

ZygCanvas::ZygCanvas(){}

ZygCanvas::ZygCanvas(QScrollArea* scrollArea):QWidget (scrollArea){
    //初始化list
    this->labelList = new QList<ZygLabel*>;

    //高度为0
    this->setGeometry(0,0,ZygCanvas::LABEL_WIDTH,0);
    displayTimer();
//    this->setBackgroundRole(QPalette::Highlight);
}


ZygCanvas::~ZygCanvas(){
    this->clearLabel();
    // 清理list
    delete this->labelList;
}

// 清理label
void ZygCanvas::clearLabel(){
//    qDebug() << "清理label";
    for(int i = 0; i < this->labelList->size(); i++){
        delete this->labelList->at(i);
    }
    this->labelList->clear();
    ZygFile::write();
}

void ZygCanvas::displayTimer(){
    // 清理全部label
    clearLabel();

    // 重设size
    this->setGeometry(0,0,ZygCanvas::LABEL_WIDTH,ZygCanvas::LABEL_HEIGHT * ZygTimer::listSize());

    for(int i = 0; i < ZygTimer::listSize(); i++){
        ZygLabel* label = new ZygLabel(ZygTimer::get(i)->toString(),this);
        label->setGeometry(0,ZygCanvas::LABEL_HEIGHT * i,ZygCanvas::LABEL_WIDTH,ZygCanvas::LABEL_HEIGHT);


        this->labelList->append(label);
        label->show();

        //点击删除
        connect(label,&ZygLabel::release,[=](){
            QDialog *dlg = new QDialog(this);
            dlg->setWindowTitle("删除闹钟");
            dlg->setFixedSize(300,240);
            ZygTimer* zygTimer = ZygTimer::get(i);
            QLabel *label = new QLabel("确定要删除" + zygTimer->toTimeString() + "的闹钟吗?",dlg);
            label->setFont(ZygFont::getFont(12));

            dlg->setPalette(ZygBackground(DELETE_BACKGROUND));

            QPushButton *ok = new QPushButton("确定",dlg);
            ok->move((dlg->width() - ok->width() * 2 - 50) / 2 , dlg->height() - ok->height() );
            connect(ok,&QPushButton::clicked,[=](){
                //删除闹钟,重新显示
                ZygTimer::deleteTimer(i);
                this->displayTimer();
                dlg->close();
            });

            QPushButton *cancel = new QPushButton("取消",dlg);
            cancel->move(ok->x() + ok->width() + 50, ok->y());
            connect(cancel,&QPushButton::clicked,[=](){
                dlg->close();
            });

            dlg->setAttribute(Qt::WA_DeleteOnClose);
            dlg->show();


        });
    }
}
